package cn.edu.zjicm.wordsnet_d.activity;

import java.util.List;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.adapter.ConsumeRecordAdapter;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.pay.Order;
import cn.edu.zjicm.wordsnet_d.synchronize.PayInterface;
import cn.edu.zjicm.wordsnet_d.synchronize.UserInterface;

public class ConsumeRecordActivity extends WordBaseActivity {
	
	private ListView recordLv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_consume_record);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("交易记录");
		
		recordLv = (ListView) findViewById(R.id.record_lv);
		
		resetOrderList();
		
		getOrderList();
	}
	
	void resetOrderList(){
		List<Order> orderList = WordFactory.getInstance(this).getOrdersLog();
		if(orderList.size()==0){
			findViewById(R.id.no_consume_tv).setVisibility(View.VISIBLE);
			recordLv.setVisibility(View.GONE);
		}else{
			recordLv.setAdapter(new ConsumeRecordAdapter(orderList, this));
		}
	}
	
	void getOrderList(){
//		getProductListHandler.sendMessage(getProductListHandler.obtainMessage(UserInterface.MSG_START));
		PayInterface.getInstance(this).getUserBought(getUserBoughtHandler);
	}
	
	Handler getUserBoughtHandler = new Handler() {
		ProgressDialog dialog2 = null;

		public void handleMessage(android.os.Message msg) {
			if (msg.what != UserInterface.MSG_START)
				if (dialog2 != null) {
					dialog2.dismiss();
					dialog2 = null;
				}
			switch (msg.what) {
			case UserInterface.MSG_START:
				dialog2 = ProgressDialog.show(ConsumeRecordActivity.this, null, "正在获取交易记录");
				break;
			case UserInterface.MSG_SUCCESS:
				resetOrderList();
				break;
			case UserInterface.MSG_UNSUCCESS:
//				Toast.makeText(RechargeActivity.this, "获取商品信息失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_BAD_NETWORK:
//				Toast.makeText(RechargeActivity.this, "网络未连接，获取商品信息失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_WEB_SITE_ERROR:
//				Toast.makeText(RechargeActivity.this, "网络未连接，获取商品信息失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			case UserInterface.MSG_WEB_SITE_ACCESS_FAILURE:
//				Toast.makeText(RechargeActivity.this, "网络未连接，获取商品信息失败，请稍后再试", Toast.LENGTH_SHORT).show();
				break;
			}
		};
	};
}
